package com.hongshan.wallet.util

import android.app.Activity
import android.content.Intent
import com.hongshan.wallet.MyApplication
import com.hongshan.wallet.base.MyActivity
import com.hongshan.wallet.service.WebSocketService
import java.util.*


/**
 * Copyright Zhangxx.
 * Copyright (c) 2019
 * All rights reserved
 *
 * @project  : Wallet
 * @package  : com.example.wallet.util
 * @author   : zhangxx
 * @date     : 2019/3/26
 * @email    : qq957413212@hotmail.com
 * @desc     : //TUDO
 */
object MyActivityManager {

    val activityStack: Stack<MyActivity<*,*>> = Stack()

    fun addActivity(activity: MyActivity<*,*>) = activityStack.add(activity)

    @Suppress("UNCHECKED_CAST")
    fun <T : MyActivity<*,*>> getActivity(clazz: Class<T>): T? {
        if (activityStack.size > 0) {
            activityStack.forEach {
                if (it.javaClass == clazz) {
                    return it as T
                }
            }
        }
        return null
    }

    fun removeActivity(activity: Activity) = activityStack.remove(activity)

    fun exitApp(){
        activityStack.forEach { it.finish() }
        val intent = Intent(MyApplication.applicationContext, WebSocketService::class.java)
        MyApplication.applicationContext.stopService(intent)
    }
}